package com.example.demo.controller;
import com.example.demo.entity.BorrowEntity;
import com.example.demo.entity.BuildingEntity;
import com.example.demo.entity.ClassroomEntity;
import com.example.demo.entity.UserEntity;
import com.example.demo.service.BuildingService;
import com.example.demo.service.ClassroomService;
import com.example.demo.service.ClassroomtypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Controller
public class ClassroomController {

    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private ClassroomtypeService classroomtypeService;

    @RequestMapping("classroomlist")
    public String classroomlist(Model model){
        List<ClassroomEntity> classroomEntities = classroomService.queryClassroomAll();
        model.addAttribute("allClassRoom",classroomEntities);
        return "classroom-list";
    }
    //跳转3D立体图显示在信息框(就是侧边栏也能显示出来，放在里面)
    @RequestMapping("class3d")
    public String class3d(){
        return "3d";
    }
    //跳转3D立体图显示在信息框(就是侧边栏也能显示出来，放在里面)
    @RequestMapping("class3dd")
    public String class3dd(){
        return "public3d";
    }


    @RequestMapping("toAddClassroom")
    public String addClassroom(Model model){
        model.addAttribute("allbuilding",buildingService.queryBuildingAll());
        model.addAttribute("allclassroomtype",classroomtypeService.queryClassroomtypeAll());
        return "classsroom-add";
    }

    @RequestMapping("addClassroom")
    @ResponseBody
    public int addClassroom(ClassroomEntity classroomEntity){
        System.out.println(classroomEntity);
        int i = classroomService.insertClassroom(classroomEntity);
        if (i>0){
            return 1;
        }else{
            return 0;
        }
    }
    @RequestMapping("seleByidClassRoom")
    public String seleByid(int cid,Model model){
        ClassroomEntity classroomEntity = classroomService.queryClassroomById(cid);
        model.addAttribute("classroom",classroomEntity);
        model.addAttribute("allbuilding",buildingService.queryBuildingAll());
        model.addAttribute("allclassroomtype",classroomtypeService.queryClassroomtypeAll());
        return "classsroom-update";
    }
    @RequestMapping("updataClassroom")
    @ResponseBody
    public int updataClassroom(ClassroomEntity classroomEntity){
        int i = classroomService.updateClassroom(classroomEntity);
        if (i>0){
            return 1;
        }else{
            return 0;
        }
    }
    @RequestMapping("deleteClassroom")
    @ResponseBody
    public int deleteClassroom(int cid){
        int i = classroomService.deleteClassroomById(cid);
        if (i>0){
            return 1;
        }else{
            return 0;
        }
    }

    //前往借教室
    @RequestMapping("toClassroomBorrow")
    public String toClassroomBorrow(HttpServletRequest request){
        List<BuildingEntity> buildingEntities = buildingService.queryBuildingAll();
        request.setAttribute("buildingEntities",buildingEntities);
//        List<ClassroomEntity> classroomEntities = classroomService.queryClassroomAll();
//        request.setAttribute("classroomEntities",classroomEntities);
        return "classroom-borrow";
    }

    //通过bid查教室楼层
    @RequestMapping("seleByBidClassRoom")
    @ResponseBody
    public List<String> seleByBid(ClassroomEntity classroomEntity){
        List<ClassroomEntity> classroomEntities = classroomService.queryClassroomByBid(classroomEntity);
        List<String> floorList = new ArrayList();
        for (ClassroomEntity c : classroomEntities) {
            String cname = c.getCname();
            String substring = cname.substring(1, 2);
            floorList.add(substring);
        }
        floorList = new ArrayList<>(new HashSet<>(floorList));
        return floorList;
    }

    //通过楼层查教室
    @RequestMapping("seleByFidClassRoom")
    @ResponseBody
    public List<ClassroomEntity> seleByFid(int fid,int bid,HttpServletRequest request){
        List<ClassroomEntity> classroomEntities = classroomService.seleByFid(fid,bid);
        request.setAttribute("classroomByFid",classroomEntities);
        return classroomEntities;
    }
    @RequestMapping("checkByCname")
    @ResponseBody
    public int checkByCname(String cname){
        ClassroomEntity classroomEntity = classroomService.checkByCname(cname);
        if (classroomEntity!=null){
            return 1;
        }else{
            return 0;
        }
    }

}